package com.tenton.service.Impl;

import com.tenton.mapper.CartMapper;
import com.tenton.pojo.ShoppingCart;
import com.tenton.service.CartService;
import com.tenton.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Date: 2021/1/20
 * @Author: Tenton
 * @Description:
 */
public class CartServiceImpl implements CartService {
    /**
     * 展示购物车中所有信息
     * @return
     */
    @Override
    public List<ShoppingCart> listCart() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        CartMapper mapper = sqlSession.getMapper(CartMapper.class);
        List<ShoppingCart> Carts = mapper.listCart();
        return Carts;
    }
    /**
     * 购物车中添加数据
     * @param shoppingCart
     * @return
     */
    @Override
    public int insertCart(ShoppingCart shoppingCart) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        CartMapper mapper = sqlSession.getMapper(CartMapper.class);
        int result = mapper.insertCart(shoppingCart);
        return result;
    }

    /**
     * 根据购物车Id删除对应数据
     * @param id
     * @return
     */
    @Override
    public int deleteCart(int id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        CartMapper mapper = sqlSession.getMapper(CartMapper.class);
        int result = mapper.deleteCart(id);
        return result;
    }

    /**
     * 根据用户Id清空购物车
     * @param userId
     * @return
     */
    @Override
    public int emptyCart(int userId) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        CartMapper mapper = sqlSession.getMapper(CartMapper.class);
        int result = mapper.emptyCart(userId);
        return result;
    }

    /**
     * 修改购物车中信息
     * @param shoppingCart
     * @return
     */
    @Override
    public boolean updateCart(ShoppingCart shoppingCart) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        CartMapper mapper = sqlSession.getMapper(CartMapper.class);
        boolean flag = mapper.updateCart(shoppingCart);
        return flag;
    }
    /**
     * 根据用户Id查询对应购物车
     * @param userId
     * @return
     */
    @Override
    public List<ShoppingCart> getCart(int userId) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        CartMapper mapper = sqlSession.getMapper(CartMapper.class);
        List<ShoppingCart> cart = mapper.getCart(userId);
        return cart;
    }
    /**
     * 根据图书名查询对应购物车
     * @param bookName
     * @return
     */
    @Override
    public ShoppingCart getCartByBookName(String bookName) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        CartMapper mapper = sqlSession.getMapper(CartMapper.class);
        ShoppingCart cart = mapper.getCartByBookName(bookName);
        return cart;
    }

    /**
     * 根据图书Id查询对应购物车
     * @param userId
     * @param bookId
     * @return
     */
    @Override
    public ShoppingCart getCartByUBId(Integer userId,Integer bookId) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        CartMapper mapper = sqlSession.getMapper(CartMapper.class);
        ShoppingCart cart = mapper.getCartByUBId(userId,bookId);
        return cart;
    }

    /**
     * 通过登录Id和图书Id查询购买数量
     * @param userId
     * @param bookId
     * @return
     */
    @Override
    public Integer getBuyNum(Integer userId, Integer bookId) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        CartMapper mapper = sqlSession.getMapper(CartMapper.class);
        int buyNum = mapper.getBuyNum(userId, bookId);
        return buyNum;
    }

    /**
     * 根据Id查询对应购物车
     * @param id
     * @return
     */
    @Override
    public ShoppingCart getCartById(int id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        CartMapper mapper = sqlSession.getMapper(CartMapper.class);
        ShoppingCart cart = mapper.getCartById(id);
        return cart;
    }
}
